#ifndef LEETCODE_CN_CPP_PROBLEMS_1221_H
#define LEETCODE_CN_CPP_PROBLEMS_1221_H

#include <stack>
#include <string>

using namespace std;

namespace problem_1221
{

class Solution
{
private:
    constexpr bool match(char a, char b)
    {
        return (a == 'L' && b == 'R') || (a == 'R' && b == 'L');
    }

public:
    int balancedStringSplit(string s)
    {
        if (s.empty())
        {
            return 0;
        }
        stack<char> stk;
        int count = 0;
        for (auto c : s)
        {
            if (stk.empty() || !match(stk.top(), c))
            {
                stk.push(c);
                continue;
            }
            stk.pop();
            if (stk.empty())
            {
                count += 1;
            }
        }
        return count;
    }
};

} // namespace problem_1221

#endif //LEETCODE_CN_CPP_PROBLEMS_1221_H
